package com.supor.aiot.mine.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.baseconfig.common.callback.OnItemClickListener;
import com.supor.aiot.common.app.AppAgent;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.utils.BitmapUtil;
import com.supor.aiot.mine.R;
import com.supor.aiot.mine.bean.ImageItem;

import java.util.List;

import androidx.core.content.res.ResourcesCompat;
import androidx.recyclerview.widget.RecyclerView;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2021, by het, ShenZhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: UploadImageAdapter <br>
 * 作者: lei <br>
 * 日期: 2021/10/29 <br>
 */
public class UploadImageAdapter extends RecyclerView.Adapter<UploadImageAdapter.UploadImageHolder> {

    private Context mContext;
    private List<ImageItem> mDataList;

    public UploadImageAdapter(Context mContext, List<ImageItem> mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
    }

    @Override
    public UploadImageAdapter.UploadImageHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view =
                LayoutInflater.from(parent.getContext()).inflate(R.layout.item_upload_image1,
                        null);
        return new UploadImageAdapter.UploadImageHolder(view);
    }

    @Override
    public void onBindViewHolder(final UploadImageAdapter.UploadImageHolder holder, int position) {
        ImageItem item = mDataList.get(position);

        if (Constants.ADD_PHOTO.equalsIgnoreCase(item.imagePath)) {
            holder.tvPhoto.setVisibility(View.VISIBLE);
            holder.tvPhoto.setText(R.string.upload_photo);
            holder.tvPhoto.setCompoundDrawablesWithIntrinsicBounds(null,
                    ResourcesCompat.getDrawable(holder.itemView.getResources(), R.mipmap.ic_photo
                            , null),
                    null, null);
            holder.icon.setImageDrawable(null);
            holder.ivDel.setVisibility(View.GONE);
            holder.ivPlay.setVisibility(View.GONE);
        } else if (Constants.ADD_VIDEO.equalsIgnoreCase(item.imagePath)) {
            holder.tvPhoto.setVisibility(View.VISIBLE);
            holder.tvPhoto.setText(R.string.upload_video);
            holder.tvPhoto.setCompoundDrawablesWithIntrinsicBounds(null,
                    ResourcesCompat.getDrawable(holder.itemView.getResources(), R.mipmap.ic_video
                            , null),
                    null, null);
            holder.icon.setImageDrawable(null);
            holder.ivDel.setVisibility(View.GONE);
            holder.ivPlay.setVisibility(View.GONE);
        } else {
            holder.tvPhoto.setVisibility(View.GONE);
            if (item.imagePath.startsWith("http")) {
                if (item.type == 1) {
                    BitmapUtil.loadVideoScreenshot(mContext, item.imagePath, holder.icon, 1000000);
                } else {
                    BitmapUtil.loadUrl(mContext, item.imagePath, holder.icon);
                }
            } else {
                BitmapUtil.loadLocal(mContext, item.imagePath, holder.icon,
                        BitmapUtil.options2);
            }
            if (item.imageDate != 1) {
                holder.ivDel.setVisibility(View.VISIBLE);
            }
            if (item.type == 0) {
                holder.ivPlay.setVisibility(View.GONE);
            } else {
                holder.ivPlay.setVisibility(View.VISIBLE);
            }
        }

        if (itemClickListener != null) {
            holder.itemView.setOnClickListener(v -> itemClickListener.onItemClick(holder.itemView
                    , holder.getLayoutPosition()));
            holder.icon.setOnClickListener(v -> itemClickListener.onItemClick(holder.icon,
                    holder.getLayoutPosition()));
            holder.ivDel.setOnClickListener(v -> itemClickListener.onItemClick(holder.ivDel,
                    holder.getLayoutPosition()));
        }
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    class UploadImageHolder extends RecyclerView.ViewHolder {
        TextView tvPhoto;
        ImageView icon;
        ImageView ivDel;
        ImageView ivPlay;

        public UploadImageHolder(View itemView) {
            super(itemView);
            if (AppAgent.getScreenWidth() > 0) {
                int width =
                        (AppAgent.getScreenWidth() - mContext.getResources().getDimensionPixelSize(R.dimen.dp_76)) / 3;
                ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width, width);
                itemView.setLayoutParams(params);
            }
            tvPhoto = itemView.findViewById(R.id.tv_ips_photo);
            icon = itemView.findViewById(R.id.iv_photo);
            ivDel = itemView.findViewById(R.id.iv_del);
            ivPlay = itemView.findViewById(R.id.iv_play);
        }
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    OnItemClickListener itemClickListener;
}
